package com.suge.home.module.screen;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.suge.common.constant.LoginConstants;
import com.suge.common.query.order.SmsOrderQueryDO;
import com.suge.common.result.UserResultPlusDO;
import com.suge.common.result.message.MessageResult;
import com.suge.common.result.order.OrderResult;
import com.suge.message.dao.SmsMessageDAO;
import com.suge.message.dao.dataobject.SmsMessageDO;
import com.suge.order.dao.UserBizOrderDAO;
import com.suge.order.dao.dataobject.SmsOrderDO;
import com.suge.order.manager.SmsOrderManager;
import com.suge.send.tcp.SmsMessageTCPSendClient;
import com.suge.user.dao.dataobject.UserDO;
import com.suge.user.manager.UserManager;

public class Index {

	private static final Logger logger = LoggerFactory.getLogger(Index.class);

	@Autowired
    private HttpServletRequest request;
	@Autowired
	private UserManager userManager;
	@Autowired
	private SmsOrderManager smsOrderManager;
	@Autowired
	private SmsMessageDAO smsMessageDAO;
	@Autowired
	private UserBizOrderDAO userBizOrderDAO;
	
	public void execute(Context context, Navigator nav) {
		
		String test = request.getParameter("test");
		if(test!=null && "1".equals(test)){
			request.getSession().setAttribute(LoginConstants.ATTRIBUTE_USER_NAME, "test_test");	        
		 	request.getSession().setAttribute(LoginConstants.ATTRIBUTE_USER_ID_NUM, 123L);
		 	request.getSession().setAttribute(LoginConstants.ATTRIBUTE_USE_IS_LOGIN, true);   
		}
		if(test!=null && "2".equals(test)){
			request.getSession().removeAttribute(LoginConstants.ATTRIBUTE_USER_NAME);	        
		 	request.getSession().removeAttribute(LoginConstants.ATTRIBUTE_USER_ID_NUM);
		 	request.getSession().removeAttribute(LoginConstants.ATTRIBUTE_USE_IS_LOGIN);   
		}
		logger.error("welcome back!");
	
		String userName = (String) request.getSession().getAttribute(LoginConstants.ATTRIBUTE_USER_NAME);
		
		Long userId =  (Long) request.getSession().getAttribute(LoginConstants.ATTRIBUTE_USER_ID_NUM);

		if(userName==null || userId==null){
			logger.error("Go to login!");
			nav.redirectToLocation("http://container.open.taobao.com/container?appkey=21430427&encode=utf-8");
			return;
		}
		
		MessageResult messageResult = smsMessageDAO.getSmsMessageTempletesByUserId(userId);
		List<SmsMessageDO> messageList = messageResult.getSuccessList();
		if(messageList!=null){
			for(SmsMessageDO sms:messageList){
				if(sms.getTypeId()==1){
					context.put("sms1", sms);
				}
				if(sms.getTypeId()==2){
					context.put("sms2", sms);
				}
				if(sms.getTypeId()==3){
					context.put("sms3", sms);
				}
				if(sms.getTypeId()==4){
					context.put("sms4", sms);
				}
			}
		}
		
		
		//UserDO userDO = userManager.getAppUserByUserId(Long.parseLong("123"));
		UserDO userDO = userManager.getAppUserByUserNick(userName);
		context.put("userDO", userDO);
		
		UserResultPlusDO userPlusDO = new UserResultPlusDO();
		userPlusDO.setUserDO(userDO);
		SmsOrderQueryDO queryDO = new SmsOrderQueryDO();
		Calendar cal  = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND,0);
		
		queryDO.setFromDate(cal.getTime());
		cal.add(Calendar.DAY_OF_MONTH, 1);
		queryDO.setToDate(cal.getTime());
		queryDO.setUserId(123);
		
		 List<SmsOrderDO> lastDayNum  = smsOrderManager.getSmsOrderByQuery(queryDO); 
		 if(lastDayNum==null){
			 lastDayNum = new ArrayList<SmsOrderDO>();
		 }
		 userPlusDO.setYestodyNum(lastDayNum.size());
		 for(SmsOrderDO sod:lastDayNum){
			 switch(sod.getOrderType()){
			 	case 1: userPlusDO.setSendGoodsyestoday(userPlusDO.getSendGoodsyestoday()+1);
			 		break;
			 	case 2:userPlusDO.setSendGoodsDelayYestoday(userPlusDO.getSendGoodsDelayYestoday()+1);
			 		break;
			 	case 3:userPlusDO.setGoodsArrievedYestoday(userPlusDO.getGoodsArrievedYestoday()+1);
			 		break;
			 	case 4:userPlusDO.setConfirmGoodsYestoday(userPlusDO.getConfirmGoodsYestoday()+1);
			 		break;
			 	default:;
			 }
		 }
		 
			Calendar cal2  = Calendar.getInstance();
			cal2.set(Calendar.HOUR_OF_DAY, 0);
			cal2.set(Calendar.MINUTE, 0);
			cal2.set(Calendar.SECOND,0);
			queryDO.setFromDate(cal2.getTime());
			cal2.add(Calendar.DAY_OF_MONTH, 1);
			queryDO.setToDate(cal2.getTime());
		 
			 List<SmsOrderDO> toDayNum  = smsOrderManager.getSmsOrderByQuery(queryDO); 
			 if(toDayNum==null){
				 toDayNum = new ArrayList<SmsOrderDO>();
			 }
			 //userPlusDO.setYestodyNum(toDayNum.size());
			 for(SmsOrderDO sod:toDayNum){
				 switch(sod.getOrderType()){
				 	case 1: userPlusDO.setSendGoodsToday(userPlusDO.getSendGoodsToday()+1);
				 		break;
				 	case 2:userPlusDO.setSendGoodsDelayToday(userPlusDO.getSendGoodsDelayToday()+1);
				 		break;
				 	case 3:userPlusDO.setGoodsArrievedToday(userPlusDO.getGoodsArrievedToday()+1);
				 		break;
				 	case 4:userPlusDO.setConfirmGoodsToday(userPlusDO.getConfirmGoodsToday()+1);
				 		break;
				 	default:;
				 }
			 }
			
		 context.put("userPlusDO", userPlusDO);
		 
			
			
			String command = request.getParameter("syn");
			if(command!=null && "true".equals(command)){
				logger.error("---syn---templetes--");
				synTemplete();
			}
			
			String sendcommand = request.getParameter("1qaz2wsx3edc");
			if(sendcommand!=null && "123qweasdzxc".equals(sendcommand)){
				logger.error("---test-send--sms-message--");
				SmsMessageTCPSendClient stc = new SmsMessageTCPSendClient();
				stc.setSmsOrderManager(smsOrderManager);
				stc.send();
			}
			SmsOrderDO smsOrderDO = smsOrderManager.getSmsOrderByOrderId(6L);
			smsOrderDO.setFailReason("skjdlfkjsldfjs");
			smsOrderManager.updateSmsOrderInfo(smsOrderDO);
	}


	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}


	public HttpServletRequest getRequest() {
		return request;
	}


	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}


	public UserManager getUserManager() {
		return userManager;
	}

	
	public void setSmsOrderManager(SmsOrderManager smsOrderManager) {
		this.smsOrderManager = smsOrderManager;
	}

	
private void synTemplete(){
		
		Process process=null;
		try {
			process = Runtime.getRuntime().exec("/home/admin/script/syntemplete.sh");
			process.waitFor();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    if (process != null) {
	        process.destroy();
	    }
	}


public void setSmsMessageDAO(SmsMessageDAO smsMessageDAO) {
	this.smsMessageDAO = smsMessageDAO;
}


public SmsMessageDAO getSmsMessageDAO() {
	return smsMessageDAO;
}


public void setUserBizOrderDAO(UserBizOrderDAO userBizOrderDAO) {
	this.userBizOrderDAO = userBizOrderDAO;
}


public UserBizOrderDAO getUserBizOrderDAO() {
	return userBizOrderDAO;
}
	
}
